package com.laiyima.zhongjiang.linghuilv.demo.home;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.laiyima.zhongjiang.linghuilv.demo.R;
import com.laiyima.zhongjiang.linghuilv.demo.util.FixedEditText;

import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

public class AddTaxActivity extends SwipeBackActivity implements View.OnClickListener {
    private FixedEditText addType,addEm,addBname,addpID,addTaxNo,addBank,addTMoblie,addAmount,addPName,addTCount;
    private FixedEditText addAddress,addPAddress,addAccount,addPMoblie;
    private LinearLayout mImageView;
    private TextView mTextView;
    private Button mButton;
    private String TAG="AddTaxActivity";
    private int addCode;
    protected boolean useThemestatusBarColor = false;//是否使用特殊的标题栏背景颜色，android5.0以上可以设置状态栏背景色，如果不使用则使用透明色值
    protected void setStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            //根据上面设置是否对状态栏单独设置颜色
            if (useThemestatusBarColor) {
                // getWindow().setStatusBarColor(getResources().getColor(R.color.colorTheme));
            } else {
                getWindow().setStatusBarColor(Color.TRANSPARENT);
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ) {//android6.0以后可以对状态栏文字颜色和图标进行修改
            getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_tax);
        addType= (FixedEditText) findViewById(R.id.add_tax_invoiceType);
        addType.setLeadText("开票类型");
        addEm= (FixedEditText) findViewById(R.id.add_tax_emailAddress);
        addEm.setLeadText("邮箱地址");
        addBname= (FixedEditText) findViewById(R.id.add_tax_billingName);
        addBname.setLeadText("公司名称");
        addpID= (FixedEditText) findViewById(R.id.add_tax_proxyId);
        addpID.setLeadText("商业ID    ");
        addTaxNo= (FixedEditText) findViewById(R.id.add_tax_taxNo);
        addTaxNo.setLeadText("税号       ");
        addBank= (FixedEditText) findViewById(R.id.add_tax_taxOpenBank);
        addBank.setLeadText("开户行   ");
        addTMoblie= (FixedEditText) findViewById(R.id.add_tax_taxMobile);
        addTMoblie.setLeadText("手机号   ");
        addAmount= (FixedEditText) findViewById(R.id.add_tax_taxAmount);
        addAmount.setLeadText("开票金额");
        addPMoblie= (FixedEditText) findViewById(R.id.add_tax_postMobile);
        addPMoblie.setLeadText("邮寄电话");
        addPName= (FixedEditText) findViewById(R.id.add_tax_postName);
        addPName.setLeadText("邮寄姓名");
        addTCount= (FixedEditText) findViewById(R.id.add_tax_taxContent);
        addTCount.setLeadText("开票内容");
        addAddress= (FixedEditText) findViewById(R.id.add_tax_taxAddress);
        addAddress.setLeadText("公司地址");
        addPAddress= (FixedEditText) findViewById(R.id.add_tax_postAddress);
        addPAddress.setLeadText("邮寄地址");
        addAccount= (FixedEditText) findViewById(R.id.add_tax_taxAccount);
        addAccount.setLeadText("对公帐号");



        mImageView= (LinearLayout) findViewById(R.id.addtax_back);
        mTextView= (TextView) findViewById(R.id.add_tax_save);
        mButton= (Button) findViewById(R.id.add_tax_save_button);

        mButton.setOnClickListener(this);
        mImageView.setOnClickListener(this);
        mTextView.setOnClickListener(this);
        setStatusBar();
    }

    @Override
    public void onClick(View v) {
        int id=v.getId();
        switch (id){
            case R.id.addtax_back:
                finish();
                break;
            case R.id.add_tax_save:
                addTax();
                break;
            case R.id.add_tax_save_button:
                addTax();
                break;
        }

    }

    private void addTax() {
        String type=addType.getText().toString();
       // Log.e(TAG, "type:::"+type);
        String bName=addBname.getText().toString();
       // Log.e(TAG, "addBname:::"+bName);
        String tAddress=addAddress.getText().toString();
       // Log.e(TAG, "addAddress:::"+tAddress);
        String tem=addEm.getText().toString();
       // Log.e(TAG, "addEm:::"+tem);
        String proxyId=addpID.getText().toString();
       // Log.e(TAG, "addpID:::"+proxyId);
        String taxNo=addTaxNo.getText().toString();
      //  Log.e(TAG, "addTaxNo:::"+taxNo);
        String tAccount=addAccount.getText().toString();
       // Log.e(TAG, "addAccount:::"+tAccount);
        String aBank=addBank.getText().toString();
       // Log.e(TAG, "addBank:::"+aBank);
        String aMoblie=addTMoblie.getText().toString();
       // Log.e(TAG, "addTMoblie:::"+aMoblie);
        String aAmount=addAmount.getText().toString();
        //Log.e(TAG, "addAmount:::"+aAmount);
        String aCount=addTCount.getText().toString();
       // Log.e(TAG, "addTCount:::"+aCount);
        String aPName=addPName.getText().toString();
        //Log.e(TAG, "addPName:::"+aPName);
        String aPAddress=addPAddress.getText().toString();
        //Log.e(TAG, "addPAddress:::"+aPAddress);
        String adPMoblie=addPMoblie.getText().toString();
        //Log.e(TAG, "addPMoblie:::"+adPMoblie);
        if (type.isEmpty()){
            Toast.makeText(getBaseContext().getApplicationContext(), "请输入发票类型", Toast.LENGTH_SHORT).show();
            return ;
        }else if (bName.isEmpty()){
            Toast.makeText(getBaseContext().getApplicationContext(), "请输入公司名称", Toast.LENGTH_SHORT).show();
            return;
        }
        else if (tAddress.isEmpty()){
            Toast.makeText(getBaseContext().getApplicationContext(), "请输入公司地址", Toast.LENGTH_SHORT).show();
            return;
        }if (tem.isEmpty()){
            Toast.makeText(getBaseContext().getApplicationContext(), "请输入邮箱地址", Toast.LENGTH_SHORT).show();
            return;
        }
        else if (proxyId.isEmpty()){
            Toast.makeText(getBaseContext().getApplicationContext(), "请输入商业ID", Toast.LENGTH_SHORT).show();
            return;
        }
        else if (taxNo.isEmpty()){
            Toast.makeText(getBaseContext().getApplicationContext(), "请输入纳税号", Toast.LENGTH_SHORT).show();
            return;
        }
        else if (tAccount.isEmpty()){
            Toast.makeText(getBaseContext().getApplicationContext(), "请输入公账号", Toast.LENGTH_SHORT).show();
            return;
        }
        else if (aBank.isEmpty()){
            Toast.makeText(getBaseContext().getApplicationContext(), "请输入开户行", Toast.LENGTH_SHORT).show();
            return;
        }
        else if (aMoblie.isEmpty()){
            Toast.makeText(getBaseContext().getApplicationContext(), "请输入开户手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        else if (aAmount.isEmpty()){
            Toast.makeText(getBaseContext().getApplicationContext(), "请输入开票金额", Toast.LENGTH_SHORT).show();
            return;
        }
        else if (aCount.isEmpty()){
            Toast.makeText(getBaseContext().getApplicationContext(), "请输入开票内容", Toast.LENGTH_SHORT).show();
            return;
        }
        else if (aPName.isEmpty()){
            Toast.makeText(getBaseContext().getApplicationContext(), "请输入邮箱姓名", Toast.LENGTH_SHORT).show();
            return;
        }
        else if (aPAddress.isEmpty()){
            Toast.makeText(getBaseContext().getApplicationContext(), "请输入邮寄地址", Toast.LENGTH_SHORT).show();
            return;
        }
        else if (adPMoblie.isEmpty()){
            Toast.makeText(getBaseContext().getApplicationContext(), "请输入邮寄电话", Toast.LENGTH_SHORT).show();
            return;
        }else{
            SharedPreferences sharedPreferences =getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE);
            String userID = sharedPreferences.getString("userID", "");
            RequestParams params = new RequestParams("http://zljl.laiima.com/index.php?s=/hjs_api/Invoice/add_invoice");
            //需要带入的参数
            params.addBodyParameter("uid", userID);
            params.addBodyParameter("invoiceType", type);
            params.addBodyParameter("billingName", bName);
            params.addBodyParameter("taxAddress", tAddress);
            params.addBodyParameter("emailAddress", tem);
            params.addBodyParameter("proxyId", proxyId);
            params.addBodyParameter("taxNo", taxNo);
            params.addBodyParameter("taxAccount", tAccount);
            params.addBodyParameter("taxOpenBank", aBank);
            params.addBodyParameter("taxMobile", aMoblie);
            params.addBodyParameter("taxAmount", aAmount);
            params.addBodyParameter("taxContent", aCount);
            params.addBodyParameter("postName", aPName);
            params.addBodyParameter("postAddress", aPAddress);
            params.addBodyParameter("postMobile", adPMoblie);

            // params.addBodyParameter("captcha", captcha);
            x.http().post(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {

                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        addCode = jsonObject.optInt("code", 0);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                   // Log.e(TAG, "保存成功" + addCode);
                   // Log.e(TAG, "保存成功" + result);
                    if (addCode == 200) {
                        Intent NewAddtaxintent=new Intent(getBaseContext().getApplicationContext(),Tax_Instead_Activity.class);
                        startActivity(NewAddtaxintent);
                        Toast.makeText(getBaseContext().getApplicationContext(), "保存成功", Toast.LENGTH_SHORT).show();
                    }
                    if (addCode == 201) {
                       // Toast.makeText(getBaseContext().getApplicationContext(), "实名认证失败", Toast.LENGTH_SHORT).show();
                    }
                    if (addCode == 500) {
                        Toast.makeText(getBaseContext().getApplicationContext(), "系统异常", Toast.LENGTH_SHORT).show();
                    }else{
                        //Toast.makeText(getBaseContext().getApplicationContext(), "信息核实不正确", Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                }
                @Override
                public void onCancelled(CancelledException cex) {

                }
                @Override
                public void onFinished() {

                }
            });
        }
    }
}
